# encoding: utf-8
from app.extensions import log
from app.modules.integration.resources import createIntegration as createI
from app import app
from app.modules.prod.resources import dbProdReport
from app.modules.projectPlan.models import ProjectPlanInfo
from app.modules.projectPlan.resources import getEndPlanList, notifyPM
from app.modules.zentao.resources import initProjectPlanInfoByProjectId, initProjectPlanMembersByProjectId


def createIntegration():
    """
    创建集成计划
    """
    log.info("============== start create integration plan scheduler ==============")
    with app.app_context():
        createI()


def dayOf3Notify():
    """
    3天内过期的项目通知
    :return:
    """
    log.info('app.modules.common.scheduler.dayOf3Notify start')
    with app.app_context():
        # _list = getEndPlanList(_day = 3)
        # notifyPM(_plans = _list)
        pass
    log.info('app.modules.common.scheduler.dayOf3Notify end')


def dayOf7Notify():
    """
    7天内过期的项目通知
    :return:
    """
    log.info('app.modules.common.scheduler.dayOf7Notify start')
    with app.app_context():
        # _list = getEndPlanList()
        # notifyPM(_plans = _list)
        pass
    log.info('app.modules.common.scheduler.dayOf7Notify end')


def updatePlanInfo():
    """
    同步项目的禅道信息
    :return:
    """
    log.info('app.modules.common.scheduler.updatePlanInfo start')
    with app.app_context():
        _list = ProjectPlanInfo.query.filter(ProjectPlanInfo.plan_status != 'ONLINE').all()
        for _plan in _list:
            if _plan.zt_id is None:
                continue
            initProjectPlanInfoByProjectId(_plan.zt_id)

        # 同步成员
        _plans = ProjectPlanInfo.query.filter(ProjectPlanInfo.plan_status != 'ONLINE').all()
        for _p in _plans:
            initProjectPlanMembersByProjectId(_p.zt_id)

    log.info('app.modules.common.scheduler.updatePlanInfo start')


def dbReport():
    """
    数据库未维护告警
    :return:
    """
    log.info('app.modules.common.scheduler.dbReport start')
    with app.app_context():
        dbProdReport()

    log.info('app.modules.common.scheduler.dbReport start')
